package org.jay.angla.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.jay.angla.adapter.SpinArrayAdapter;
import org.jay.angla.onlab.OnlabApplication;
import org.jay.angla.onlab.R;
import org.jay.angla.onlab.R.array;
import org.jay.angla.onlab.R.id;
import org.jay.angla.onlab.R.layout;
import org.jay.angla.onlab.R.string;
import org.jay.angla.utils.Contants;
import org.jay.angla.utils.SharePreferenceUtil;
import org.jay.angla.utils.ToastShow;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

@EActivity(R.layout.activity_ts_setting)
public class TSSettingActivity extends Activity {

	private ToastShow mToastShow;

	@ViewById(R.id.et_wavelength)
	EditText mWavelengthEditText;
	@ViewById(R.id.et_start_time)
	EditText mStartTimeEditText;
	@ViewById(R.id.et_end_time)
	EditText mEndTimeEditText;
	@ViewById(R.id.et_up_limit)
	EditText mUpLimitEditText;
	@ViewById(R.id.et_down_limit)
	EditText mDownLimitEditText;
	@ViewById(R.id.tv_down_abs)
	TextView mDownTextView;
	@ViewById(R.id.tv_up_abs)
	TextView mUpTextView;
	@ViewById(R.id.sp_time_interval)
	Spinner mTimeIntervalSpinner;
	@ViewById(R.id.rb_test_mode_abs)
	RadioButton mAbsModeButton;
	@ViewById(R.id.rb_test_mode_trans)
	RadioButton mTransModeButton;
	@ViewById(R.id.rg_test_mode)
	RadioGroup mTestModeRadioGroup;
	@ViewById(R.id.btn_ts_sure)
	Button mSureButton;
	@ViewById(R.id.btn_ts_cancel)
	Button mCancelButton;

	public static final int TEST_MODE_ABS = 0;
	public static final int TEST_MODE_TRANS = 1;

	private float mWavelength;
	private int mStartTime;
	private int mEndTime;
	private float mInterval;
	private int mTestMode;
	private float mUpLimit;
	private float mDownLimit;

	@AfterViews
	public void init_ui() {
		// set time interval
		String[] mStringArray = getResources().getStringArray(
				R.array.time_interval);
		ArrayAdapter<String> adapter = new SpinArrayAdapter(this, mStringArray);
		mTimeIntervalSpinner.setAdapter(adapter);

		mTimeIntervalSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int pos, long id) {
						// TODO Auto-generated method stub
						String[] value = getResources().getStringArray(
								R.array.time_interval);
						mInterval = Float.parseFloat(value[pos]);
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});

		mTestModeRadioGroup
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						int id = group.getCheckedRadioButtonId();
						if (id == R.id.rb_test_mode_abs) {
							mTestMode = TEST_MODE_ABS;
							mUpTextView.setText("Abs");
							mDownTextView.setText("Abs");
						} else if (id == R.id.rb_test_mode_trans) {
							mTestMode = TEST_MODE_TRANS;
							mUpTextView.setText(" %T ");
							mDownTextView.setText(" %T ");
						}
					}
				});

		mSureButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				if ((mWavelengthEditText.getEditableText().toString().length() < 1)
						|| (mStartTimeEditText.getEditableText().toString()
								.length() < 1)
						|| (mEndTimeEditText.getEditableText().toString()
								.length() < 1)
						|| (mUpLimitEditText.getEditableText().toString()
								.length() < 1)
						|| (mDownLimitEditText.getEditableText().toString()
								.length() < 1)) {
					mToastShow
							.toastShow(getString(R.string.input_correct_param));
					return;
				}

				mWavelength = Float.parseFloat(mWavelengthEditText
						.getEditableText().toString());
				mUpLimit = Float.parseFloat(mUpLimitEditText.getEditableText()
						.toString());
				mDownLimit = Float.parseFloat(mDownLimitEditText
						.getEditableText().toString());
				mStartTime = Integer.parseInt(mStartTimeEditText
						.getEditableText().toString());
				mEndTime = Integer.parseInt(mEndTimeEditText.getEditableText()
						.toString());

				intent.putExtra("wavelength", mWavelength);
				intent.putExtra("uplimit", mUpLimit);
				intent.putExtra("downlimit", mDownLimit);
				intent.putExtra("starttime", mStartTime);
				intent.putExtra("endtime", mEndTime);
				intent.putExtra("testmode", mTestMode);
				intent.putExtra("interval", mInterval);
				setResult(99, intent);

				SharePreferenceUtil sp = OnlabApplication.getInstance()
						.getSpUtil();
				sp.setTestMode(mTestMode);
				sp.setWavelength(mWavelength);
				sp.setStartTime(mStartTime);
				sp.setEndTime(mEndTime);
				sp.setUpLimit(mUpLimit);
				sp.setDownLimit(mDownLimit);
				sp.setInterval(mInterval);

				finish();
			}
		});

		mCancelButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		loadDefaultSetting();
	}

	private void loadDefaultSetting() {
		SharePreferenceUtil sp = OnlabApplication.getInstance().getSpUtil();
		mTestMode = sp.getTestMode();
		mWavelength = sp.getWavelength();
		mStartTime = sp.getStartTime();
		mEndTime = sp.getEndTime();
		mInterval = sp.getInterval();
		mUpLimit = sp.getUpLimit();
		mDownLimit = sp.getDownLimit();

		if (mTestMode == TEST_MODE_ABS) {
			mAbsModeButton.setChecked(true);
			mTransModeButton.setChecked(false);
		} else {
			mAbsModeButton.setChecked(false);
			mTransModeButton.setChecked(true);
		}
		mWavelengthEditText.setText("" + mWavelength);
		mStartTimeEditText.setText("" + mStartTime);
		mEndTimeEditText.setText("" + mEndTime);
		mUpLimitEditText.setText("" + mUpLimit);
		mDownLimitEditText.setText("" + mDownLimit);
		String[] intervalStrings = getResources().getStringArray(
				R.array.time_interval);
		int index = 0;
		for (int i = 0; i < intervalStrings.length; i++) {
			if (Float.parseFloat(intervalStrings[i]) == mInterval) {
				index = i;
				break;
			}
		}
		if (index >= intervalStrings.length) {
			mTimeIntervalSpinner.setSelection(0);
		} else {
			mTimeIntervalSpinner.setSelection(index);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		mToastShow = new ToastShow(this);
	}
}
